package br.mg.bhe.ftpobre.gui.fs;

import br.mg.bhe.ftpobre.ConnectionData;
import br.mg.bhe.ftpobre.Main;
import br.mg.bhe.ftpobre.fs.FileNameGenerator;
import java.net.URL;
import java.util.Collections;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;

import br.mg.bhe.ftpobre.fs.IFileLocalizer;
import br.mg.bhe.ftpobre.fs.LocalFile;
import br.mg.bhe.ftpobre.fs.LocalFileLocalizer;
import br.mg.bhe.ftpobre.fs.RemoteFile;
import br.mg.bhe.ftpobre.fs.RemoteFileLocalizer;
import br.mg.bhe.ftpobre.fs.compare.CompareLocalFilesByLogicalType;
import br.mg.bhe.ftpobre.fs.compare.CompareRemoteFilesByLogicalType;
import br.mg.bhe.ftpobre.fs.data.xml.XmlFacade;
import br.mg.bhe.ftpobre.gui.MainFrame;
import br.mg.bhe.ftpobre.util.FileSizeConverter;
import br.mg.bhe.ftpobre.util.LocalFileUtil;
import java.io.File;

/**
 * 
 * @author Aureliano
 */
public final class FileTableModelBuilder {

	private static final String[] columnNames = new String[] {
			"", "Nome", "Tamanho"
		};
	
	private FileTableModelBuilder() {
		super();
	}
	
	public static TableModel createLocalFilesTableModel(String path) {
		return new DefaultTableModel(createLocalFilesData(path), columnNames) {
			
			@Override
			public Class<?> getColumnClass(int columnIndex) {
				if (columnIndex == 0) {
					return ImageIcon.class;
				} else {
					return super.getColumnClass(columnIndex);
				}
			}
			
			@Override
			public boolean isCellEditable(int row, int column) {
				return false;
			}
		};
	}
	
	public static TableModel createRemoteFilesTableModel(String path) {
            TableModel model = null;
            ConnectionData c = Main.getConnectionData();
            if ((c.isConnected()) && (c.isWorkingOnline())) {
                model = new DefaultTableModel(createRemoteFilesData(path), columnNames) {
			
                    @Override
                    public Class<?> getColumnClass(int columnIndex) {
                        if (columnIndex == 0) {
                            return ImageIcon.class;
                        } else {
                            return super.getColumnClass(columnIndex);
                        }
                    }

                    @Override
                    public boolean isCellEditable(int row, int column) {
                            return false;
                    }
		};
            } else {
                model =  createRemoteFilesFromLocalXmlModel(path);
            }
            
            return model;
	}
	
	private static Object[][] createLocalFilesData(String path) {
		IFileLocalizer<LocalFile> localizer = new LocalFileLocalizer();
		List<LocalFile> files = localizer.loadFiles(path);
		Collections.sort(files, new CompareLocalFilesByLogicalType());
		Object[][] data = new Object[files.size() + 1][3];
		ImageIcon folderIcon = getFolderIcon();
		
		data[0][0] = folderIcon;
		data[0][1] = LocalFile.getUpFolder();
		data[0][2] = null;
		
		for (int i = 0; i < files.size(); i++) {
			LocalFile f = files.get(i);
			data[i+1][0] = (f.isDirectory()) ? folderIcon : null;
			data[i+1][1] = f;
			data[i+1][2] = FileSizeConverter.convertFileSize(f.getSize());
		}
		
		return data;
	}
	
	private static Object[][] createRemoteFilesData(String path) {
            IFileLocalizer<RemoteFile> localizer = new RemoteFileLocalizer();
            List<RemoteFile> files = localizer.loadFiles(path);
            Collections.sort(files, new CompareRemoteFilesByLogicalType());
            Object[][] data = new Object[files.size() + 1][3];
            ImageIcon folderIcon = getFolderIcon();

            data[0][0] = folderIcon;
            data[0][1] = RemoteFile.getUpFolder();
            data[0][2] = null;

            for (int i = 0; i < files.size(); i++) {
                RemoteFile f = files.get(i);
                data[i+1][0] = (f.isDirectory()) ? folderIcon : null;
                data[i+1][1] = f;
                data[i+1][2] = FileSizeConverter.convertFileSize(f.getFile().getSize());
            }

            return data;
	}

        private static TableModel createRemoteFilesFromLocalXmlModel(String path) {
            String name = FileNameGenerator.generateFileName(MainFrame.getInstance().getAbsolutePathFromRemoteFilePath()) + ".xml";
            File file = new File(LocalFileUtil.getCacheFolder().getAbsolutePath() + "/" + name);

            try {
                return XmlFacade.readTableModel(file);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
	
	private static ImageIcon getFolderIcon() {
		URL url = FileTableModelBuilder
				.class.getClassLoader()
				.getResource("br/mg/bhe/ftpobre/img/folder.png");
		return new ImageIcon(url);
	}
}